// 子模块moduleA路径：store/modules/moduleA.js
export default {
  // 命名空间
  namespaced: true,
  state: {
    token: "",
  },
  getters: {},
  mutations: {
    setToken(state, payload) {
      state.token = payload;
    },
  },
  actions: {
    async fetchToken({commit}) {
      // 异步获取token
      // 调用wx.getUserProfile来获取用户信息 这里注意getUserProfile只能先调用
      const [err1, result1] = await uni.getUserProfile({
        desc: "提升体验",
      });
      // 调用wx.login来获取code
      const [err2, { code }] = await uni.login();
      // 调用后台登录接口 获取token
      const result3 = await uni.$u.post("/users/wxlogin", {
        ...result1,
        code,
      });
      console.log(105, result3); // 登录成功 获取到token
      const { token } = result3.message;
      commit('setToken',token)
      // const token =
      //   "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjE3LCJpYXQiOjE2NjU5ODg1MzUsImV4cCI6MTAwMTY2NTk4ODUzNH0.VM4CBB8PHjgwBYeH4mWkJZ2AVwAnzdFlO2LRQO48XyU";
    },
  },
};
